package cn.lg.soar.boot.fc.service.impl;

import cn.lg.soar.boot.client.entity.ClientUser;
import cn.lg.soar.boot.client.service.ClientUserService;
import cn.lg.soar.boot.fc.entity.Process;
import cn.lg.soar.boot.fc.entity.ProcessApply;
import cn.lg.soar.boot.fc.entity.Room;
import cn.lg.soar.boot.fc.entity.TenancyRoom;
import cn.lg.soar.boot.fc.entity.vo.RepairSubmitVo;
import cn.lg.soar.boot.fc.service.*;
import cn.lg.soar.common.data.msg.I18n;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.system.auth.entity.User;
import cn.lg.soar.system.auth.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.boot.fc.mapper.RepairMapper;
import cn.lg.soar.boot.fc.entity.Repair;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 报修表
 * @author ZhuanZ（无密码）
 * @since 2025-01-22
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class RepairServiceImpl extends BaseServiceImpl<RepairMapper, Repair> implements RepairService {

    @Autowired
    protected TenancyRoomService tenancyRoomService;
    @Autowired
    private RoomService roomService;
    @Autowired
    protected IUserService userService;
    @Autowired
    protected ProcessService processService;
    @Resource
    private ProcessApplyService processApplyService;
    /** 客户端用户信息服务 */
    @Autowired private ClientUserService clientUserService;

    private SqueryWapper<Process> squeryWapperProcess;

    private SqueryWapper<Repair> squeryWapper;

    /**
     * 报修
     */
    @Transactional
    public HttpResult<Void> repair(RepairSubmitVo repairSubmit) {
        // 更新租房情况
        TenancyRoom tenancyRoom = tenancyRoomService.getById(repairSubmit.getTenancyRoomId());
        if (null == tenancyRoom){
            return HttpResult.fail("B0000", I18n.build("租房信息为空！"));
        }
        tenancyRoom.setTenancyState(6);
        AssertUtil.isTrue(tenancyRoomService.updateById(tenancyRoom), "更新失败");
        Room room = roomService.getById(tenancyRoom.getRoomId());
        if(room != null){
            room.setUseState(3);
            AssertUtil.isTrue(roomService.updateById(room), "更新失败");
        }
        Repair repair = new Repair();
        BeanUtils.copyProperties(repairSubmit, repair);
        repair.setRepairerUserId(tenancyRoom.getUserId());
        repair.setRoomId(tenancyRoom.getRoomId());
        repair.setRepairTime(LocalDateTime.now());
        repair.setExamineState(0);
        repair.setRepairState(0);
        AssertUtil.isTrue(super.save(repair), "新增失败");

//        User user = userService.getById(tenancyRoom.getUserId());
//        if (null == user){
//            System.out.println("===============1申请用户为空！");
//            return HttpResult.fail("B0000", I18n.build("申请用户为空！"));
//        }
        ClientUser user = clientUserService.getById(tenancyRoom.getUserId());
        if(user == null) {
            return HttpResult.fail("B0000", I18n.build("申请用户为空！"));
        }
        if (squeryWapperProcess == null) {
            squeryWapperProcess = new SqueryWapper<>(Process.class);
        }
        QueryWrapper<Process> wrapper = Wrappers.query();
        List<String> f = new ArrayList<>();
        f.add("examineType-eq~3");
        squeryWapperProcess.setWhere(f, wrapper);
        List<Process> list = processService.list(wrapper);
        if(list.size() <= 0){
            System.out.println("===============1维修审批流程为空！");
            return HttpResult.fail("B0000", I18n.build("维修审批流程为空！"));
        }
        if (squeryWapper == null) {
            squeryWapper = new SqueryWapper<>(Repair.class);
        }
        QueryWrapper<Repair> wrapper1 = Wrappers.query();
        List<String> f1 = new ArrayList<>();
        f1.add("tenancyRoomId-eq~"+tenancyRoom.getId());
        f1.add("repairerUserId-eq~"+tenancyRoom.getUserId());
        f1.add("examineState-eq~0");
        f1.add("repairState-eq~0");
        List<String> orderBy = new ArrayList<>();
        orderBy.add("-repairTime");
        squeryWapper.setWhere(f1, wrapper1);
        squeryWapper.setOrderBy(orderBy, wrapper1);
        List<Repair> list1 = super.list(wrapper1);

        ProcessApply apply = new ProcessApply();
        apply.setTenancyRoomId(tenancyRoom.getId());
        apply.setRepairId(list1.get(0).getId());
        apply.setUserId(user.getId());
        apply.setUserName(user.getUserName());
        apply.setProcessId(list.get(0).getId());
        apply.setProcessName(list.get(0).getProcessName());
        return processApplyService.applyExamine(apply);
    }

    /**
     * 维修
     */
    @Transactional
    public HttpResult<Void> maintain(Repair repair) {
        Repair repair1 = super.getById(repair.getId());
        if (null == repair1){
            return HttpResult.fail("B0000", I18n.build("报修信息为空！"));
        }
//        // 更新租房情况
//        TenancyRoom tenancyRoom = tenancyRoomService.getById(repair1.getTenancyRoomId());
//        if (null == tenancyRoom){
//            return HttpResult.fail("B0000", I18n.build("租房信息为空！"));
//        }
//        tenancyRoom.setTenancyState(3);
//        AssertUtil.isTrue(tenancyRoomService.updateById(tenancyRoom), "更新失败");
//        Room room = roomService.getById(tenancyRoom.getRoomId());
//        if(room != null){
//            room.setUseState(2);
//            AssertUtil.isTrue(roomService.updateById(room), "更新失败");
//        }
//        repair.setUpkeepTime(LocalDateTime.now());
        repair.setRepairState(2);
        AssertUtil.isTrue(super.updateById(repair), "更新失败");
        return HttpResult.success();
    }

    /**
     * 维修完成
     */
    @Transactional
    public HttpResult<Void> maintainComplete(Long repairId) {
        Repair repair = super.getById(repairId);
        if (null == repair){
            return HttpResult.fail("B0000", I18n.build("报修信息为空！"));
        }
        // 更新租房情况
        TenancyRoom tenancyRoom = tenancyRoomService.getById(repair.getTenancyRoomId());
        if (null == tenancyRoom){
            return HttpResult.fail("B0000", I18n.build("租房信息为空！"));
        }
        tenancyRoom.setTenancyState(3);
        AssertUtil.isTrue(tenancyRoomService.updateById(tenancyRoom), "更新失败");
        Room room = roomService.getById(tenancyRoom.getRoomId());
        if(room != null){
            room.setUseState(2);
            AssertUtil.isTrue(roomService.updateById(room), "更新失败");
        }
        repair.setUpkeepTime(LocalDateTime.now());
        repair.setRepairState(1);
        AssertUtil.isTrue(super.updateById(repair), "更新失败");
        return HttpResult.success();
    }
}
